//
//  AppDelegate.m
//  RongIMDemo
//
//  Created by 崔逢举 on 2020/2/5.
//  Copyright © 2020 崔逢举. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseNavigationController.h"
#import "BaseViewController.h"
#import "DemoTabbarController.h"


#define kAPPKEY @"25wehl3u2gpxw"
@interface AppDelegate ()<RCIMConnectionStatusDelegate,RCIMReceiveMessageDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //初始化IM
    [self configRongIM];
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    DemoTabbarController *welcomeVC = [[DemoTabbarController alloc] init];
    self.window.rootViewController = [[BaseNavigationController alloc] initWithRootViewController:welcomeVC];
    [self.window makeKeyAndVisible];
    return YES;
}

//初始化融云IM
- (void)configRongIM {
    [[RCIM sharedRCIM] initWithAppKey:kAPPKEY];
    //设置导航server和文件server地址 私有云配置才需要
//    [[RCIMClient sharedRCIMClient] setServerInfo:@"导航服务器地址" fileServer:@"文件服务器地址"];
    [RCIM sharedRCIM].showUnkownMessage = YES;
    //开启用户信息和群组信息的持久化
    [RCIM sharedRCIM].enablePersistentUserInfoCache = YES;
    [[RCIM sharedRCIM] setConnectionStatusDelegate:self];

    //开启输入状态监听
    [RCIM sharedRCIM].enableTypingStatus = YES;
    //是否开启消息撤回
    [RCIM sharedRCIM].enableMessageRecall = YES;
    [[RCIM sharedRCIM] setReceiveMessageDelegate:self];
    [RCIM sharedRCIM].globalNavigationBarTintColor = UIColorFromRGB(0x1E8DFE);
    //开启多端未读状态同步
    [RCIM sharedRCIM].enableSyncReadStatus = YES;
   
    //连接IM  测试数据
    [self connectIMWithToken:@"JMZqKTlXbrcbOu9XcQShroL9RvlmbAmtFivn17Wu0juhDo+DwqToBQ==@9z83.cn.rongnav.com;9z83.cn.rongcfg.com" userName:@"老王" portraitUrl:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1588826697826&di=f9ca7127ea2afdb11eacd7d48d382c81&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_pic%2F19%2F04%2F10%2Fdab3098005616f281ae30dd25b62d3af.jpg"];
}

//连接IM
- (void)connectIMWithToken:(NSString *)token userName:(NSString *)userName portraitUrl:(NSString *)portraitUrl {
    [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
        [RCIM sharedRCIM].currentUserInfo = [[RCUserInfo alloc]initWithUserId:userId name:userName portrait:portraitUrl];
        //设置后接收方会自动获取该用户信息并显示
        [RCIM sharedRCIM].enableMessageAttachUserInfo = YES;
    } error:^(RCConnectErrorCode status) {
        
    } tokenIncorrect:^{
        NSLog(@"connectIMWithToken error:tokenIncorrect");
    }];
}



#pragma mark - RCIMReceiveMessageDelegate

- (void)onRCIMReceiveMessage:(RCMessage *)message left:(int)left {

}

- (BOOL)onRCIMCustomAlertSound:(RCMessage *)message {
    return NO;
}

#pragma mark - RCIMConnectionStatusDelegate
/**
 *  网络状态变化。
 *
 *  @param status 网络状态。
 */
- (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
    if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) {
        //被挤掉
    } else if (status == ConnectionStatus_TOKEN_INCORRECT) {
        
    } else if (status == ConnectionStatus_DISCONN_EXCEPTION) {
        [[RCIMClient sharedRCIMClient] disconnect];
    }
}
@end
